import { Request } from "../request";
import { handleAuthError, handleNetworkError } from './tool'

// 实例化axios类
const instance = new Request({
  // baseURL: '',
  timeout: 1000 * 60 * 5,
  interceptors: {
    // 实例请求拦截器
    requestInterceptors: config => {
      console.log('实例请求拦截器 instance request');

      return config
    },
    // 实例响应拦截器
    responseInterceptors(result: any) {
      console.log('实例响应拦截器 instance response', result, result.status);
      if (result.status !== 200) return result.data
      handleAuthError(result.data.errno)
      return result
    },
    responseInterceptorsCatch(err) {
      handleNetworkError(err.response.status)
      Promise.reject(err.response)
    },
  }
})

// 取消请求
export const cancelRequest = (url: string | string[]) => {
  return instance.cancelRequest(url)
}
// 取消全部请求
export const cancelAllRequest = () => {
  return instance.cancelAllRequest()
}

export default instance
